'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2019-12-19 20:47:58
'''
import pymysql
import json
final = []
conn = pymysql.connect(
    host = "172.19.8.66",
    port = 3306,
    user = "algorithm",
    password = "S4!4Eywru@aH",
    database = "rcd_phoenix_admin",
    charset='utf8'
    )

cursor = conn.cursor()

# sql = "show tables"
# cursor.execute(sql)
# results = cursor.fetchall()
# print(results)


'''
@note: deal the feature_storage_mapping tabel
'''
req_fea = ["feature_code", "feature_storage_id", "ns", "ns_id"]
req_ind = []
sql = "desc feature_storage_mapping"
cursor.execute(sql)

results = cursor.fetchall()
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_storage_mapping"
cursor.execute(sql)
results = list(cursor.fetchall())

# print(results)
for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_storage_mapping_final = [[a[i] for i in req_ind] for a in final]
# for f in feature_storage_mapping_final:
#     print(f)



'''
@note: deal the feature_storage tabel
'''
final = []
req_fea = ['id', 'cluster_name', 'type', 'address']
req_ind = []
sql = "desc feature_storage"
# sql = "show tables"
cursor.execute(sql)

results = cursor.fetchall()
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1

final.append(index)
sql = "select * from feature_storage"
cursor.execute(sql)
results = list(cursor.fetchall())

# print(results)
for result in results:
    # print(result)
    final.append(list(result))

# print(final)

feature_storage_final = [[a[i] for i in req_ind] for a in final]
# for f in feature_storage_final:
#     print(f)
feature_storage_file = []
for f in feature_storage_final:
    if f[2] == 1:
        feature_storage_file.append([f[0], f[3]])

res_map = {}
for f in feature_storage_file:
    res_map[f[0]] = f[1]
res_map_redis = json.dumps(res_map)
res_map_redis = res_map_redis.replace(', "', ',\n"')
# print("res_map = ", res_map)
f_res = open('./redis_map', 'w')
f_res.write(res_map_redis)
f_res.close()



'''
@note: deal the feature_v2
'''
final = []
req_fea = ['code', 'key_type', 'value_type', 'ns']
req_ind = []
sql = "desc feature_v2"
# sql = "show tables"
cursor.execute(sql)

results = cursor.fetchall()
# print(results)
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1

final.append(index)
sql = "select * from feature_v2"
cursor.execute(sql)
results = list(cursor.fetchall())

# print(results)
for result in results:
    # print(result)
    final.append(list(result))

# for f in final[0:3]:
#     print(f)
feature_storage_final = [[a[i] for i in req_ind] for a in final]
# for f in feature_storage_final:
#     print(f)
tmp_res = {}
for i in range(1, len(feature_storage_final)):
    tmp_col = feature_storage_final[i]
    tmp_res[tmp_col[0]] = {'key_type':tmp_col[1], 'value_type': tmp_col[2], 'ns': tmp_col[3]}


# res_map = json.dumps(tmp_res)
# res_map = res_map.replace('}, "', '},\n"')
# # print("res_map = ", res_map)
# f_res = open('./feature_store', 'w')
# f_res.write(res_map)
# f_res.close()

tmp_keys = res_map.keys()
# print(tmp_keys)

for i in range(1, len(feature_storage_mapping_final)):
    tmp_use = feature_storage_mapping_final[i]
    if str(tmp_use[1]) not in tmp_keys and tmp_use[1] not in tmp_keys:
        continue
    # print(tmp_use[0])
    tmp_res[tmp_use[0]]['feature_storage_id'] = tmp_use[1]
deletes = []
for k, v in tmp_res.items():
    if 'feature_storage_id' not in v:
        deletes.append(k)
for d in deletes:
    del tmp_res[d]

# test_list=[]
for k, v in tmp_res.items():
    if v['feature_storage_id'] == 10:
        v['feature_storage_id'] = 6
        # test_list.append(k)
res_map_fea_store = json.dumps(tmp_res)
res_map_fea_store = res_map_fea_store.replace('}, "', '},\n"')
# print("res_map = ", res_map)
f_res = open('./feature_store', 'w')
f_res.write(res_map_fea_store)
f_res.close()

# print(test_list)
